//
//  JiaocaiCell.m
//  Learn
//
//  Created by chijianguo on 2024/3/29.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "JiaocaiCell.h"

@implementation JiaocaiCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
        [self setSelectionStyle:(UITableViewCellSelectionStyleNone)];
    }
    
    return self;
    
}

- (void)setupUI {
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = COLOR_WHITE;
    bgView.layer.cornerRadius = 15;
    bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(10);
        make.right.equalTo(self.contentView).offset(-10);
        make.top.bottom.equalTo(self.contentView).offset(0);
    }];

    self.imView = [[UIImageView alloc] init];
    self.imView.layer.cornerRadius = 15;
    self.imView.layer.masksToBounds = YES;
    [bgView addSubview:self.imView];

    self.jiaocaiBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.jiaocaiBtn setImage:[UIImage imageNamed:@"jiantou"] forState:(UIControlStateNormal)];
    [bgView addSubview:self.jiaocaiBtn];
    [self.jiaocaiBtn addTarget:self action:@selector(jiaocaiAction:) forControlEvents:(UIControlEventTouchUpInside)];

    self.titleLB =  [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.titleLB.titleLabel.font = FONT_BODY_16;
    [self.titleLB setTitleColor:rgbHex(0x333333, 1) forState:UIControlStateNormal];;
    [self.titleLB addTarget:self action:@selector(jiaocaiAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [bgView addSubview:self.titleLB];
    
    self.subTitleLB = [[UILabel alloc] init];
    self.subTitleLB.font = [UIFont systemFontOfSize:14];
    self.subTitleLB.textColor = rgbHex(0x8e8e8e, 1);
    self.subTitleLB.numberOfLines = 0;
    [bgView addSubview:self.subTitleLB];

    self.moreBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [self.moreBtn setTitle:@"更换单元" forState:(UIControlStateNormal)];
    self.moreBtn.titleLabel.font = FONT_BODY_12;
    self.moreBtn.tintColor = rgbHex(0x0CB76C, 1);
    self.moreBtn.layer.cornerRadius = 12;
    self.moreBtn.layer.masksToBounds = YES;
    self.moreBtn.layer.borderWidth = 1;
    self.moreBtn.layer.borderColor = rgbHex(0x0CB76C, 1).CGColor;
    [bgView addSubview:self.moreBtn];
    [self.moreBtn addTarget:self action:@selector(moreAction:) forControlEvents:(UIControlEventTouchUpInside)];

    [self.imView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(bgView).offset(10);
        make.bottom.equalTo(bgView).offset(-10);
        make.width.mas_equalTo(86);
    }];
    [self.jiaocaiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgView).offset(-5);
        make.top.equalTo(self.imView).offset(10);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    [self.titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.jiaocaiBtn.mas_left).offset(-10);
        make.left.equalTo(self.imView.mas_right).offset(10);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(self.jiaocaiBtn.mas_centerY);
    }];
    [self.subTitleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.jiaocaiBtn.mas_left).offset(-10);
        make.left.equalTo(self.imView.mas_right).offset(10);
        make.top.equalTo(self.titleLB.mas_bottom).offset(5);
        make.height.mas_equalTo(40);
    }];
    [self.moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgView).offset(-10);
        make.bottom.equalTo(bgView).offset(-10);
        make.size.mas_equalTo(CGSizeMake(72, 24));
    }];

}

- (void)jiaocaiAction:(UIButton *)sender {
    if(self.jiaocaiBlock){
        self.jiaocaiBlock();
    }
}

- (void)moreAction:(UIButton *)sender {
    if(self.changeBlock){
        self.changeBlock();
    }
}

@end
